package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.dto.TroubleCarInfoDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Member;

import net.sf.json.JSONObject;

public class CarQRCodePrintCheckAjaxAction extends TmsAjaxAction {
	private Logger logger = Logger.getLogger(CarQRCodePrintCheckAjaxAction.class);

	@Override
	public void process(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws TmsException {
		logger.debug(this.getClass().getName() + "#execute.");
		String r = "true";
		String reason = "其他类型";
		String vin = request.getParameter("vin");
		String memberId = request.getParameter("memberId");
		if(!StringUtil.isNullOrBlank(vin)){
			TxController controller = TxController.getTxController();
			try {
				controller.begin();
				Car carInfo = Car.getInstance();
				carInfo = carInfo.findCarByVin(vin);
				if(carInfo.getAttestStatus() == 1){
					r = "false";
					reason = "【U8024】此车辆为未检测车辆，不能发放'放心车贴'";
				}else if(carInfo.getAttestStatus() == 3){
					r = "false";
					reason = "【U8024】此车辆为三类车车辆，不能发放'放心车贴'";
				}else if(carInfo.getAttestStatus() == 4){
					r = "false";
					reason = "【U8024】此车辆为大事故车车辆，不能发放'放心车贴'";
				}else if(carInfo.getAttestStatus() == 5){
					r = "false";
					reason = "【U8024】此车辆为水淹车车辆，不能发放'放心车贴'";
				}else if(carInfo.getAttestStatus() == 6){
					r = "false";
					reason = "【U8024】此车辆为火烧车车辆，不能发放'放心车贴'";
				}
				
				Member member = new Member();
				member = member.getMember(memberId);
				int contractType = member.getContractType();
				if(contractType == 2){
					r = "false";
					reason = "【U8025】此车辆为非签约商户车辆,不能发放'放心车贴'";
				}
				controller.commit();
			} catch (Exception e) {
				controller.rollback();
				logger.error("CarQRCodePrintCheckAjaxAction exception,", e);
			}
		}
		JSONObject json =new JSONObject(); 
		json.put("result", r);
		json.put("reason", reason);
		responseValues(response, json.toString() , TEXT);
		logger.debug(this.getClass().getName() + "#doAction was completed.");
	}
}
